home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / GSIMAGE1.C < prev    next >
C/C++ Source or Header  |  1993-05-17  |  16KB  |  543 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsimage1.c */
  20. /* Image procedures for Ghostscript library */
  21. /* This file is logically part of gsimage.c; we have split it out */
  22. /* to reduce the code working set. */
  23. #include "gx.h"
  24. #include "memory_.h"
  25. #include "gpcheck.h"
  26. #include "gserrors.h"
  27. #include "gxfixed.h"
  28. #include "gxarith.h"
  29. #include "gxmatrix.h"
  30. #include "gscspace.h"
  31. #include "gspaint.h"
  32. #include "gzstate.h"
  33. #include "gzdevice.h"            /* requires gsstate.h */
  34. #include "gzcolor.h"            /* requires gxdevice.h */
  35. #include "gzpath.h"
  36. #include "gxcolor.h"
  37. #include "gxcpath.h"
  38. #include "gxdevmem.h"
  39. #include "gximage.h"
  40.  
  41. /* Process the next piece of an image */
  42. int
  43. gs_image_next(register gs_image_enum *penum, byte *dbytes, uint dsize)
  44. {    uint rsize = penum->bytes_per_row;
  45.     uint pos = penum->byte_in_row;
  46.     int width = penum->width;
  47.     uint dleft = dsize;
  48.     uint dpos = 0;
  49.     gs_state *pgs = penum->pgs;
  50.     gx_device *save_dev = 0;
  51.     fixed xcur_save, ycur_save;
  52.     int y_save;
  53.     int code;
  54.     /* Accumulate separated colors, if needed */
  55.     if ( penum->plane_index == 0 )
  56.         penum->plane_size = dsize;
  57.     else if ( dsize != penum->plane_size )
  58.         return_error(gs_error_undefinedresult);
  59.     penum->planes[penum->plane_index] = dbytes;
  60.     if ( ++(penum->plane_index) != penum->spread )
  61.         return 0;
  62.     penum->plane_index = 0;
  63.     /* Save the dynamic state components in case of an error. */
  64.     xcur_save = penum->xcur;
  65.     ycur_save = penum->ycur;
  66.     y_save = penum->y;
  67.     /* We've accumulated an entire set of planes. */
  68.     if ( !penum->never_clip )
  69.        {    /* Install the clipping device if needed. */
  70.         gx_device *dev = (gx_device *)&penum->clip_dev;
  71.         save_dev = gs_currentdevice(pgs);
  72.         penum->clip_dev.target = save_dev;
  73.         gx_set_device_only(pgs, dev);
  74.        }
  75.     while ( dleft )
  76.        {    /* Fill up a row, then display it. */
  77.         uint bcount = min(dleft, rsize - pos);
  78.         byte *bptr =
  79.           penum->buffer + (pos << 3) / penum->bps * penum->spread;
  80.         int px;
  81.         for ( px = 0; px < penum->spread; px++ )
  82.           (*penum->unpack)(penum, &penum->map[px], bptr + px, penum->planes[px] + dpos, bcount, pos);
  83.         pos += bcount;
  84.         dpos += bcount;
  85.         dleft -= bcount;
  86.         if ( pos == rsize )    /* filled an entire row */
  87.            {
  88. #ifdef DEBUG
  89. if ( gs_debug['B'] )
  90.    {            int i, n = width * penum->spp;
  91.             dputs("[B]row:");
  92.             for ( i = 0; i < n; i++ )
  93.                 dprintf1(" %02x", penum->buffer[i]);
  94.             dputs("\n");
  95.    }
  96. #endif
  97.             if ( !penum->skewed )
  98.               { /* Precompute integer y and height, */
  99.                 /* and check for clipping. */
  100.                 fixed yc = penum->ycur, yn;
  101.                 fixed dyy = penum->fyy;
  102.                 fixed adjust = penum->adjust;
  103.                 if ( dyy > 0 )
  104.                   dyy += adjust << 1,
  105.                   yc -= adjust;
  106.                 else
  107.                   dyy = (adjust << 1) - dyy,
  108.                   yc -= dyy - adjust;
  109.                 if ( yc >= penum->clip_box.q.y ) goto mt;
  110.                 yn = yc + dyy;
  111.                 if ( yn <= penum->clip_box.p.y ) goto mt;
  112.                 penum->yci = fixed2int_var_rounded(yc);
  113.                 penum->hci =
  114.                   fixed2int_var_rounded(yn) - penum->yci;
  115.                 if ( penum->hci == 0 ) goto mt;
  116.               }
  117.             code = (*penum->render)(penum, penum->buffer, width * penum->spp, 1);
  118.             if ( code < 0 ) goto err;
  119. mt:            if ( ++(penum->y) == penum->height ) goto end;
  120.             pos = 0;
  121.             penum->xcur += penum->fyx;
  122.             penum->ycur += penum->fyy;
  123.            }
  124.        }
  125.     penum->byte_in_row = pos;
  126.     code = 0;
  127.     goto out;
  128. end:    /* End of data */
  129.     code = 1;
  130.     goto out;
  131. err:    /* Error or interrupt, restore original state. */
  132.     penum->plane_index = penum->spread - 1;
  133.     penum->xcur = xcur_save;
  134.     penum->ycur = ycur_save;
  135.     penum->y = y_save;
  136.     /* Note that caller must call gs_image_cleanup */
  137.     /* for both error and normal termination. */
  138. out:    if ( save_dev != 0 ) gx_set_device_only(pgs, save_dev);
  139.     return code;
  140. }
  141.  
  142. /* Clean up by releasing the buffers. */
  143. void
  144. gs_image_cleanup(register gs_image_enum *penum)
  145. {    if ( penum->buffer )
  146.     {    gs_free((char *)penum->buffer, 1, penum->buffer_size,
  147.             "image buffer");
  148.         penum->buffer = 0;
  149.     }
  150.     if ( penum->line )
  151.     {    gs_free((char *)penum->line, 1, penum->line_size,
  152.             "image line");
  153.         penum->line = 0;
  154.     }
  155. }
  156.  
  157. /* ------ Unpacking procedures ------ */
  158.  
  159. void
  160. image_unpack_1(const gs_image_enum *penum, const sample_map *pmap,
  161.   byte *bptr, register const byte *data, uint dsize, uint inpos)
  162. {    register ulong *bufp = (unsigned long *)bptr;
  163.     int left = dsize;
  164.     register const ulong *map = &pmap->table.lookup4x1to32[0];
  165.     register uint b;
  166.     if ( left & 1 )
  167.        {    b = data[0];
  168.         bufp[0] = map[b >> 4];
  169.         bufp[1] = map[b & 0xf];
  170.         data++, bufp += 2;
  171.        }
  172.     left >>= 1;
  173.     while ( left-- )
  174.        {    b = data[0];
  175.         bufp[0] = map[b >> 4];
  176.         bufp[1] = map[b & 0xf];
  177.         b = data[1];
  178.         bufp[2] = map[b >> 4];
  179.         bufp[3] = map[b & 0xf];
  180.         data += 2, bufp += 4;
  181.        }
  182. }
  183.  
  184. void
  185. image_unpack_2(const gs_image_enum *penum, const sample_map *pmap,
  186.   byte *bptr, register const byte *data, uint dsize, uint inpos)
  187. {    register ushort *bufp = (unsigned short *)bptr;
  188.     int left = dsize;
  189.     register const ushort *map = &pmap->table.lookup2x2to16[0];
  190.     while ( left-- )
  191.        {    register unsigned b = *data++;
  192.         *bufp++ = map[b >> 4];
  193.         *bufp++ = map[b & 0xf];
  194.        }
  195. }
  196.  
  197. void
  198. image_unpack_4(const gs_image_enum *penum, const sample_map *pmap,
  199.   register byte *bufp, register const byte *data, uint dsize, uint inpos)
  200. {    register int spread = penum->spread;
  201.     int left = dsize;
  202.     register const byte *map = &pmap->table.lookup8[0];
  203.     while ( left-- )
  204.        {    register unsigned b = *data++;
  205.         *bufp = map[b >> 4]; bufp += spread;
  206.         *bufp = map[b & 0xf]; bufp += spread;
  207.        }
  208. }
  209.  
  210. void
  211. image_unpack_8(const gs_image_enum *penum, const sample_map *ignore_pmap,
  212.   byte *bufp, const byte *data, uint dsize, uint inpos)
  213. {    if ( data != bufp ) memcpy(bufp, data, dsize);
  214. }
  215.  
  216. /* ------ Rendering procedures ------ */
  217.  
  218. /* Rendering procedure for ignoring an image.  We still need to iterate */
  219. /* over the samples, because the procedure might have side effects. */
  220. int
  221. image_render_skip(gs_image_enum *penum, byte *data, uint w, int h)
  222. {    return h;
  223. }
  224.  
  225. /* Rendering procedure for a monobit image with no */
  226. /* skew or rotation and pure colors. */
  227. int
  228. image_render_simple(gs_image_enum *penum, byte *buffer, uint w, int h)
  229. {    byte *line = penum->line;
  230.     uint line_size, line_width;
  231.     gx_device *dev = penum->pgs->device->info;
  232.     dev_proc_copy_mono((*copy_mono)) = dev->procs->copy_mono;
  233.     int ix = fixed2int_rounded(penum->xcur);
  234.     const int iy = penum->yci, ih = penum->hci;
  235.     gx_color_index
  236.         zero = penum->icolor0.color1,
  237.         one = penum->icolor1.color1;
  238.     int dy;
  239.  
  240.     if ( penum->map[0].table.lookup4x1to32[0] != 0 )
  241.         zero = penum->icolor1.color1,
  242.         one = penum->icolor0.color1;
  243.  
  244.     if ( line == 0 )
  245.     {    /* A direct BitBlt is possible. */
  246.         line = buffer;
  247.         line_size = (w + 7) >> 3;
  248.         line_width = w;
  249.     }
  250.     else
  251.     {    fixed xl = penum->xcur + fixed_half - int2fixed(ix);
  252.         const fixed dxx = penum->fxx;
  253.         const fixed dxx_4 = dxx << 2;
  254.         const fixed dxx_8 = dxx_4 << 1;
  255.         register const byte *psrc = buffer;
  256.         byte sbit = 0x80;
  257.         byte *endp = buffer + (w >> 3);
  258.         const byte endbit = 1 << (~w & 7);
  259.         byte data;
  260.  
  261.         line_size = penum->line_size;
  262.         line_width = penum->line_width;
  263.         if ( dxx < 0 )
  264.             ix -= line_width,
  265.             xl += int2fixed(line_width);
  266.  
  267.         /* Invert the bit following the last valid data bit. */
  268.         if ( endbit == 0x80 ) *endp = ~endp[-1] << 7;
  269.         else if ( *endp & (endbit << 1) ) *endp &= ~endbit;
  270.         else *endp |= endbit;
  271.         data = *psrc;
  272.  
  273.         memset(line, 0, line_size);
  274.         /*
  275.          * Loop invariants:
  276.          *    data = *psrc;
  277.          *    sbit = 1 << n, 0<=n<=7.
  278.          */
  279.         do
  280.         {    int x0, n, bit;
  281.             byte *bp;
  282.             static const byte lmasks[9] =
  283.              { 0xff, 0x7f, 0x3f, 0x1f, 0xf, 7, 3, 1, 0 };
  284.             static const byte rmasks[8] =
  285.              { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
  286.  
  287.             /* Scan a run of zeros. */
  288.             while ( ~data & sbit )
  289.             {    xl += dxx;
  290.                 sbit >>= 1;
  291.             }
  292.             if ( !sbit )
  293.             {    while ( (data = *++psrc) == 0 )
  294.                     xl += dxx_8;
  295.                 if ( data > 0xf )
  296.                     sbit = 0x80;
  297.                 else
  298.                     xl += dxx_4,
  299.                     sbit = 0x08;
  300.                 while ( ~data & sbit )
  301.                 {    xl += dxx;
  302.                     sbit >>= 1;
  303.                 }
  304.             }
  305.             if ( !(psrc < endp || sbit > endbit) )
  306.                 break;
  307.             x0 = fixed2int_var(xl);
  308.  
  309.             /* Scan a run of ones. */
  310.             while ( data & sbit )
  311.             {    xl += dxx;
  312.                 sbit >>= 1;
  313.             }
  314.             if ( !sbit )
  315.             {    while ( (data = *++psrc) == 0xff )
  316.                     xl += dxx_8;
  317.                 if ( data < 0xf0 )
  318.                     sbit = 0x80;
  319.                 else
  320.                     xl += dxx_4,
  321.                     sbit = 0x08;
  322.                 while ( data & sbit )
  323.                 {    xl += dxx;
  324.                     sbit >>= 1;
  325.                 }
  326.             }
  327.  
  328.             /* Fill the run in the scan line. */
  329.             n = fixed2int_var(xl) - x0;
  330.             if ( n < 0 ) x0 += n, n = -n;
  331.             bp = line + (x0 >> 3);
  332.             bit = x0 & 7;
  333.             if ( (n += bit) <= 8 )
  334.                 *bp |= lmasks[bit] - lmasks[n];
  335.             else
  336.             {    *bp++ |= lmasks[bit];
  337.                 if ( n > 64 )
  338.                 {    int nb = (n >> 3) - 1;
  339.                     memset(bp, 0xff, nb);
  340.                     bp += nb;
  341.                     n &= 7;
  342.                 }
  343.                 else
  344.                 {    n -= 8;
  345.                     while ( (n -= 8) >= 0 )
  346.                         *bp++ = 0xff;
  347.                 }
  348.                 *bp |= rmasks[n & 7];
  349.             }
  350.  
  351.         } while ( psrc < endp || sbit > endbit );
  352.     }
  353.  
  354.     /* Finally, transfer the scan line to the device. */
  355.     for ( dy = 0; dy < ih; dy++ )
  356.         (*copy_mono)(dev, line, 0, line_size, gx_no_bitmap_id,
  357.             ix, iy + dy, line_width, 1, zero, one);
  358.  
  359.     return_check_interrupt(1);
  360. }
  361.  
  362. /* Rendering procedure for the general case of displaying a */
  363. /* monochrome image, dealing with multiple bit-per-sample images, */
  364. /* general transformations, and arbitrary single-component */
  365. /* color spaces (DeviceGray, CIEBasedA, Separation, Indexed). */
  366. /* This procedure handles a single scan line. */
  367. int
  368. image_render_mono(gs_image_enum *penum, byte *buffer, uint w, int h)
  369. {    gs_state *pgs = penum->pgs;
  370.     const int masked = penum->masked;
  371.     const fixed dxx = penum->fxx;
  372.     fixed xt = penum->xcur;
  373.     gs_color_space *pcs = pgs->color_space;
  374.     cs_proc_remap_color((*remap_color)) = pcs->type->remap_color;
  375.     gs_client_color cc;
  376.     cmap_proc_gray((*map_gray)) = pgs->cmap_procs->map_gray;
  377.     gx_device_color *pdevc = pgs->dev_color;
  378.     /* Make sure the cache setup matches the graphics state. */
  379.     /* Also determine whether all tiles fit in the cache. */
  380.     int tiles_fit = gx_check_tile_cache(pgs);
  381. #define image_set_gray(sample_value)\
  382.    { pdevc = &penum->dev_colors[sample_value];\
  383.      switch ( pdevc->halftone_level )\
  384.       { default:        /* halftone */\
  385.       if ( !tiles_fit ) gx_color_load(pdevc, pgs); break;\
  386.         case -1:        /* not computed yet */\
  387.       if ( penum->device_color )\
  388.         (*map_gray)(byte2frac(sample_value), pdevc, pgs);\
  389.       else\
  390.       { decode_sample(sample_value, cc, 0);\
  391.         (*remap_color)(&cc, pcs, pdevc, pgs);\
  392.       }\
  393.     case 0: ;        /* pure color */\
  394.       }\
  395.    }
  396.     fixed xl = xt;
  397.     register const byte *psrc = buffer;
  398.     byte *endp = buffer + w;
  399.     fixed xrun = xt;        /* x at start of run */
  400.     register byte run = *psrc;    /* run value */
  401.     int htrun =            /* halftone run value */
  402.       (masked ? 255 : -2);
  403.  
  404.     *endp = ~endp[-1];    /* force end of run */
  405.     if ( penum->slow_loop )
  406.       { /* Skewed, or imagemask with a halftone. */
  407.         const fixed
  408.           dxy = penum->fxy, dyx = penum->fyx,
  409.           dyy = penum->fyy;
  410.         fixed ytf = penum->ycur;
  411.         fixed yrun = ytf;
  412.         int code;
  413.         for ( ; ; )
  414.           { if ( *psrc++ != run )
  415.           { /* Fill the region between xrun and xl. */
  416.             if ( run != htrun )
  417.               { if ( run == 0 )
  418.               { if ( masked ) goto trans;
  419.               }
  420.             htrun = run;
  421.             image_set_gray(run);
  422.               }
  423.             code = gz_fill_pgram_fixed(xrun, yrun, xl - xrun,
  424.                            ytf - yrun, dyx, dyy,
  425.                            pdevc, pgs);
  426.             if ( code < 0 ) return code;
  427. trans:            if ( psrc > endp ) break;
  428.             yrun = ytf;
  429.             xrun = xl;
  430.             run = psrc[-1];
  431.           }
  432.         xl += dxx;
  433.         ytf += dxy;        /* harmless if no skew */
  434.           }
  435.       }
  436.     else            /* fast loop */
  437.       { /* No skew, and not imagemask with a halftone. */
  438.         const fixed adjust = penum->adjust;
  439.         fixed xa = (dxx >= 0 ? adjust : -adjust);
  440.         const int yt = penum->yci, iht = penum->hci;
  441.         gx_device *dev = pgs->device->info;
  442.         dev_proc_fill_rectangle((*fill_proc)) = dev->procs->fill_rectangle;
  443.         dev_proc_tile_rectangle((*tile_proc)) = dev->procs->tile_rectangle;
  444.         dev_proc_copy_mono((*copy_mono_proc)) = dev->procs->copy_mono;
  445.         dev_proc_copy_color((*copy_color_proc)) = dev->procs->copy_color;
  446.         /* If each pixel is likely to fit in a single halftone tile, */
  447.         /* determine that now (tile_offset = offset of row within tile). */
  448.         int tile_offset =
  449.           gx_check_tile_size(pgs,
  450.                  fixed2int_rounded(any_abs(dxx) + (xa << 1)),
  451.                  yt, iht);
  452.         /* Fold the adjustment into xrun and xl, */
  453.         /* including the +0.5 for rounding. */
  454.         xrun = xrun - xa + fixed_half;
  455.         xl = xl + xa + fixed_half;
  456.         xa <<= 1;
  457.         for ( ; ; )
  458.           { /* Skip large constant regions quickly, */
  459.             /* but don't slow down transitions too much. */
  460.             while ( psrc[0] == run )
  461.         { if ( psrc[1] == run )
  462.           { if ( psrc[2] == run )
  463.             { if ( psrc[3] == run )
  464.               { psrc += 4, xl += dxx << 2;
  465.             continue;
  466.               }
  467.               else
  468.                 psrc += 3, xl += (dxx << 1) + dxx;
  469.             }
  470.             else
  471.               psrc += 2, xl += dxx << 1;
  472.           }
  473.           else
  474.             psrc++, xl += dxx;
  475.           break;
  476.         }
  477.         psrc++;
  478.           { /* Fill the region between xrun and xl. */
  479.             int xi = fixed2int_var(xrun);
  480.             int wi = fixed2int_var(xl) - xi;
  481.             int tsx, code;
  482.             gx_bitmap *tile;
  483.             if ( wi <= 0 )
  484.               { if ( wi == 0 ) goto mt;
  485.             xi += wi, wi = -wi;
  486.               }
  487.             switch ( run )
  488.               {
  489.               case 0:
  490.             if ( masked ) goto mt;
  491.             if ( !color_is_pure(&penum->icolor0) ) goto ht;
  492.             code = (*fill_proc)(dev, xi, yt, wi, iht, penum->icolor0.color1);
  493.             break;
  494.               case 255:        /* just for speed */
  495.             if ( !color_is_pure(&penum->icolor1) ) goto ht;
  496.             code = (*fill_proc)(dev, xi, yt, wi, iht, penum->icolor1.color1);
  497.             break;
  498.               default:
  499. ht:            /* Use halftone if needed */
  500.             if ( run != htrun )
  501.               { image_set_gray(run);
  502.                 htrun = run;
  503.               }
  504.             /* We open-code gz_fill_rectangle_open, */
  505.             /* because we've done some of the work for */
  506.             /* halftone tiles in advance. */
  507.             if ( color_is_pure(pdevc) )
  508.               { code = (*fill_proc)(dev, xi, yt, wi, iht, pdevc->color1);
  509.               }
  510.             else if ( tile_offset >= 0 &&
  511.                   (tile = pdevc->tile,
  512.                    (tsx = (xi + pgs->phase_mod.x) % tile->rep_width) + wi <= tile->size.x)
  513.                 )
  514.               { /* The pixel(s) fit(s) in a single tile. */
  515.                 byte *row = tile->data + tile_offset;
  516.                 code = (color_is_color_halftone(pdevc) ?
  517.                     (*copy_color_proc)
  518.                       (dev, row, tsx, tile->raster, gx_no_bitmap_id,
  519.                        xi, yt, wi, iht) :
  520.                     (*copy_mono_proc)
  521.                       (dev, row, tsx, tile->raster, gx_no_bitmap_id,
  522.                        xi, yt, wi, iht,
  523.                        pdevc->color1, pdevc->color2)
  524.                     );
  525.                 return_if_interrupt();
  526.               }
  527.             else
  528.               { code = (*tile_proc)(dev, pdevc->tile, xi, yt, wi, iht,
  529.                          pdevc->color1, pdevc->color2,
  530.                          pgs->phase_mod.x, pgs->phase_mod.y);
  531.               }
  532.               }
  533.             if ( code < 0 ) return code;
  534. mt:            if ( psrc > endp ) break;
  535.             xrun = xl - xa;    /* original xa << 1 */
  536.             run = psrc[-1];
  537.           }
  538.         xl += dxx;
  539.           }
  540.       }
  541.     return 1;
  542. }
  543.